package com.czl.module_work.viewModel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ApplyResultBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class MyInviteViewModel(application: MyApplication, model: DataRepository): BaseViewModel<DataRepository>(application, model) {
    val uc = UiChangeEvent()
    val guestNo = ObservableField<String>()
    val reservationTime = ObservableField<String>()
    val approveStatus = ObservableField<String>()
    val qrCodeImage = ObservableField<String>()
    class UiChangeEvent {
        val doTransferFormClick: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val btnTransferFormClick: BindingCommand<Void> = BindingCommand(BindingAction {
        uc.doTransferFormClick.call()
    })

    fun getApplyResult(applyId: String){
        model.getApplyResult(applyId, model.getUserData()?.loginUser?.phone?:"")
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ApplyResultBean>>() {
                override fun onResult(t: BaseBean<ApplyResultBean>) {
                    if (t.code == 200) {
                        guestNo.set(t.data?.guestNo?:"")
                        reservationTime.set(t.data?.reservationTime?:"")
                        qrCodeImage.set(t.data?.qrCodeImage?:"")
                        approveStatus.set(when(t.data?.approveStatus){
                            "1"->{
                                "待审核"
                            }
                            "2"->{
                                "待拜访"
                            }
                            "3"->{
                                "驳回"
                            }
                            "4"->{
                                "通过前撤销"
                            }
                            "5"->{
                                "通过之后撤销"
                            }
                            "6"->{
                                "已完成"
                            }
                            else->{
                                ""
                            }
                        })
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }
            })
    }
}